<?php
namespace App\Model;
use Think\Model;
class BaseModel extends Model 
{
	public function get_doctor($data, $split=',')
	{
        if(!is_array($data)){
        	$data = array_filter(explode($split, $data));
        }
        $res = M('doctor')->where(array('id'=>array('in', $data)))->select();
        return $res;
	}

	/**
	 * @param
	 * $condition   查询条件
	 * $order       order by
	 * $limit       limit条数 
	 * $title 		array标题限制长度,省略符  
	 * $content 	array内容限制长度,省略符 
	 * $ofield      所需字段
	 * @return [array] [文章列表]
	 */
	public function get_article($condition, $order, $limit, $title, $content, $ofield){
		// $where['a.pass'] 	= array('eq', 1);
		// $where['a.Publish'] 	= array('eq', 1);
		// $where['a.Home'] 	= array('eq', 1);
		$where['a.State']    = array('eq', 1);
		if(is_array($condition)){
			$where = array_merge((array)$where, $condition);
		}
		$order = $order ? $order : "a.ArticleTime desc,a.id desc";
		$field = 'a.id,a.Title,a.BTitle,a.Class,a.Content,a.VideoTime,a.Thumbnails,b.className,c.name,c.picture,c.id as cid';
		if($ofield){
			$field .= ','.$ofield; 
		}
		$data = M('Article a')->field($field)->join('left join '.C('DB_PREFIX').'article_type b on b.id=a.Class left join '.C('DB_PREFIX').'doctor c on c.id=a.doctor_id left join '.C('DB_PREFIX').'article_type d on d.id=b.fatherId')->where($where)->order($order)->limit($limit)->select();
		if(is_array($title)){
			foreach ($data as $key => $value) {
				$data[$key]['Title'] = cut_str(trim($value['Title']), $title['length'], 0, 'UTF-8', $title['omitted']);
			}
		}
		if(is_array($content)){
			foreach ($data as $k => $val) {
				$data[$k]['Content'] = cut_str(trim(strip_tags($val['Content'])), $content['length'], 0, 'UTF-8', $content['omitted']);
			}
		}
		return $data;
	}
	
	/**
	 * 根据顶级分类返回所有子分类
	 */
	public function getFather($data, $fatherId=0, $isClear=TRUE)
	{
	    static $ret = array();
	    if($isClear){
	        $ret = array();
	        $ret[] = $fatherId;
	    }
	    foreach ($data as $k => $v)
	    {
	        if($v['fatherId'] == $fatherId)
	        {
	            $ret[] = $v['id'];
	            $this->getFather($data, $v['id'], FALSE);
	        }
	    }
	    return $ret;
	}

	/**
	 * 获取疾病
	 */
	public function getDisease($condition){
		$model = M('disease');
		$data = $model->field('id,title,Focus')->where($condition)->select();
		return $data;
	}

	/**
	 * 获取咨询问题
	 */
	public function get_ask($where, $limit, $content, $title){
	 	$data = M('zixun','DB_','DB_CONFIG1')->field('id,question,title,submittime,username,bingqing_menu_2,img')->where($where)->order('id desc')->limit($limit)->select();
		foreach ($data as $key => $value) {
			$data[$key]['content'] = M('replay','DB_','DB_CONFIG1')->where('bingliid='.$value['id'])->getField('content');
			$data[$key]['submittime'] = json_decode( json_encode($value['submittime']), true);
			$data[$key]['bingqing_menu_2'] = reset(array_filter(explode('|',$value['bingqing_menu_2'])));
		}
		if(is_array($content)){
			foreach ($data as $k => $val) {
				$data[$k]['content'] = cut_str(trim(strip_tags($val['content'])), $content['length'], 0, 'UTF-8', $content['omitted']);
			}
		}
		if(is_array($title)){
			foreach ($data as $k => $val) {
				$data[$k]['title'] = cut_str(trim(strip_tags($val['title'])), $title['length'], 0, 'UTF-8', $title['omitted']);
			}
			foreach ($data as $k => $val) {
				$data[$k]['question'] = cut_str(trim(strip_tags($val['question'])), $title['length'], 0, 'UTF-8', $title['omitted']);
			}
		} 
		return $data;
	}
}
























